package com.cloudz.iptv.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import com.cloudz.iptv.R;
import com.cloudz.iptv.fragment.BaseFragment;
import com.owen.focus.FocusBorder;

import butterknife.ButterKnife;

public abstract class BaseActivity extends AppCompatActivity implements BaseFragment.FocusBorderHelper
{
    protected FocusBorder focusBorder;

    protected Toast toast;

    public abstract int getLayoutId();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        //隐藏bar
        if (getSupportActionBar() != null)
        {
            getSupportActionBar().hide();
        }

        super.onCreate(savedInstanceState);

        // 设置布局
        setContentView(getLayoutId());

        //初始化注解插件
        ButterKnife.bind(this);

        // 设置焦点
        initFocusBorderAsDrawable();

        // 设置监听
        boundGlobalFocusListener();

        toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
        //设置对齐方式
        toast.setGravity(Gravity.CENTER, 0, 0);
    }

    /**
     * 图片焦点框
     */
    public void initFocusBorderAsDrawable()
    {
        /** 图片焦点框 */
        focusBorder = new FocusBorder.Builder().asDrawable()
                .borderDrawableRes(R.mipmap.focus)
                .titleBackgroundRes(R.drawable.shape_item_title_bg)
                .titleMarginBottomAutoAlignBorder()
                .titleTextColor(Color.LTGRAY)
                .build(this);


    }

    /**
     * 绑定事件
     */

    public void boundGlobalFocusListener()
    {

        focusBorder.boundGlobalFocusListener(new FocusBorder.OnFocusCallback()
        {
            @Override
            public FocusBorder.Options onFocus(View oldFocus, View newFocus)
            {
                return createColorBorderOptions(10);
            }
        });
    }

    public void showToast(String str)
    {
        toast.setText(str);
        toast.show();
    }


    public void showToastError()
    {
        toast.setText("服务返回数据异常");
        toast.show();
    }

    private FocusBorder.Options createColorBorderOptions(int radius)
    {
        float scale = 1.05f;
        return FocusBorder.OptionsFactory.get(scale, scale);
    }

    @Override
    public FocusBorder getFocusBorder()
    {
        return focusBorder;
    }
}
